﻿using System.Windows;
using System.Windows.Controls;
using XDCCManager.Model;

namespace XDCCManager.Pages
{
	/// <summary>
	/// Interaction logic for ServedFilesManager.xaml
	/// </summary>
	public partial class ServedFilesManager : UserControl
	{
		public ServedFilesManager()
		{
			InitializeComponent();
		}

		public Popups.PackEditor Editor
		{
			get { return (Popups.PackEditor)GetValue(EditorProperty); }
			set { SetValue(EditorProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Editor.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty EditorProperty =
			DependencyProperty.Register("Editor", typeof(Popups.PackEditor), typeof(ServedFilesManager), new UIPropertyMetadata(null));

		private void Up_Click(object sender, RoutedEventArgs e)
		{
			Model.Packs.IPack source = packList.SelectedPack;
			if (source != null)
			{
				Model.Packs.IPackList list = (FindResource("ServedFilesContextDataSource") as ServedFilesContext).ViewPacks;
				Model.Actions.IAction action = list.MovePack(source, Model.Packs.MoveDirection.Up);
				(FindResource("ActionQueueContextDataSource") as Model.ActionQueueContext).AddAction(action);
			}
		}

		private void Add_Click(object sender, RoutedEventArgs e)
		{
			Model.Files.File source = fileList.SelectedFile;
			if (source != null)
			{
				Model.Packs.IPackList list = (FindResource("ServedFilesContextDataSource") as ServedFilesContext).ViewPacks;
				Model.Actions.IAction action = list.AddFile(source);
				(FindResource("ActionQueueContextDataSource") as Model.ActionQueueContext).AddAction(action);
			}
		}

		private void Down_Click(object sender, RoutedEventArgs e)
		{
			Model.Packs.IPack source = packList.SelectedPack;
			if (source != null)
			{
				Model.Packs.IPackList list = (FindResource("ServedFilesContextDataSource") as ServedFilesContext).ViewPacks;
				Model.Actions.IAction action = list.MovePack(source, Model.Packs.MoveDirection.Down);
				(FindResource("ActionQueueContextDataSource") as Model.ActionQueueContext).AddAction(action);
			}
		}

		private void Remove_Click(object sender, RoutedEventArgs e)
		{
			Model.Packs.IPack source = packList.SelectedPack;
			if (source != null)
			{
				Model.Packs.IPackList list = (FindResource("ServedFilesContextDataSource") as ServedFilesContext).ViewPacks;
				Model.Actions.IPackAction action = list.DeletePack(source) as Model.Actions.IPackAction;
				(FindResource("ActionQueueContextDataSource") as Model.ActionQueueContext).AddAction(action);
				packList.SelectedPack = action.Change;
			}
		}
	}
}
